<?php

namespace App\Models\Api;

use App\Models\Api\AwardUser;
use App\Models\Api\Base;
use App\Models\Api\Fortunenote;
use App\Models\Api\Orders;
use App\Models\Api\Recode;

class Fortune extends Base
{

    protected $table   = 'fortune';
    public $timestamps = false;

    /**
     * 获取当前用户是否解锁的当前运势
     * @param array $map
     * @throws \ApiException
     */
    public function getUserUnlockFortune($map = [])
    {
        $time = $map['time'];
        $rid  = $map['rid'];
        $uid  = $map['uid'];

        $AwardUserModel   = new AwardUser();
        $OrdersModel      = new Orders();
        $RecodeModel      = new Recode();
        $FortuneNoteModel = new FortuneNote();

        $is_unlock = false; //当前日期运势是否已解锁
        $time1     = strtotime("-3days", $time);
        $time2     = strtotime("-2days", $time);
        $time3     = strtotime("-1days", $time);
        $time4     = strtotime("+1days", $time);
        $time5     = strtotime("+2days", $time);
        $time6     = strtotime("+3days", $time);

        $times = [
            1 => $time1,
            2 => $time2,
            3 => $time3,
            4 => $time,
            5 => $time4,
            6 => $time5,
            7 => $time6,
        ];
        // 1,当前日期为今天或者是12月25-12月31
        $time_range = prDates(['start' => '2018-12-24', 'end' => '2018-12-31', 'type' => 1]);

        if ($time == strtotime(date('Y-m-d', time())) || in_array(date('Y-m-d', $time), $time_range)) {

            $is_unlock = true;
        }

        // 获取档案购买的订单
        $fortuneOrders = $OrdersModel->fortune_orders(['uid' => $uid, 'rid' => $rid, 'type' => 1]);

        $fortune1 = [];
        $fortune2 = [];

        if (!empty($fortuneOrders)) {
            foreach ($fortuneOrders as $k => $v) {
                // 当前档案已购买解锁，解锁运势
                if (in_array($rid, explode(',', $v['rids']))) {
                    $extime = json_decode($v['extend'], true)['expire_time'];
                    if (isset($extime) || !empty($extime)) {
                        $v['expire_time'] = $extime;
                    }
                    $start_time = strtotime(date('Y-m-d', $v['time']));
                    if ($v['expire_time']) {
                        $end_time = strtotime('-1 days', strtotime($v['expire_time'], $v['time']));
                    } else {
                        $end_time = $v['time'];
                    }
                    // 判断前后三天是否已解锁,获取标识
                    foreach ($times as $t) {
                        if ($start_time <= $t && $t <= strtotime(date('Y-m-d', $end_time))) {
                            $fortune1[] = strval(date('d', $t));
                        }
                    }
                    // 当前档案 已购买解锁，解锁运势
                    if (!$is_unlock && $time >= $start_time && $time <= $end_time) {
                        $is_unlock = true;
                    }
                }
            }
        }

        // 获取当前用户的档案id
        $selfId = $RecodeModel->getSelfRecode(['uid' => $uid]);
        // 获取中奖纪录
        $prize = $AwardUserModel->get_award_fortune(['uid' => $uid, 'rid' => $rid, 'type' => 1, 'self_id' => $selfId]);

        if (!empty($prize)) {
            foreach ($prize as $k => $v) {
                $extend     = json_decode($v['extend'], true);
                $start_time = strtotime(date('Y-m-d', $extend['award_time']));
                $end_time   = strtotime('-1 days', strtotime($v['expire_time'], $extend['award_time']));
                // 判断前后三天是否已解锁,获取标识
                foreach ($times as $t) {
                    if ($start_time <= $t
                        && $t <= strtotime(date('Y-m-d', $end_time))) {
                        $fortune2[] = strval(date('d', $t));
                    }
                }
                // 当前档案已中奖，解锁运势
                if (!$is_unlock && $time >= $start_time && $time <= $end_time) {
                    $is_unlock = true;
                }
            }
        }

        $fortune3 = [];
        if (date('m', $time) == '12') {
            $fortune3[] = "25";
            $fortune3[] = "26";
            $fortune3[] = "27";
            $fortune3[] = "28";
            $fortune3[] = "29";
            $fortune3[] = "30";
            $fortune3[] = "31";
        }

        $fortune = array_merge($fortune1, $fortune2, $fortune3);

        // 获取运势日记
        $note_list = [];
        $data_time = $FortuneNoteModel->getRidNote(['rid' => $rid]);
        foreach ($data_time as $v) {
            if (in_array($v['time'], $times)) {
                $note_list[] = date('d', $v['time']);
            }
        }

        // 获取七天时间的日记标识
        $seven = [];

        $note_status = 0;

        // 获取当前用户的档案id
        $selfId = $RecodeModel->getSelfRecode(['uid' => $uid]);

        foreach ($note_list as $d) {
            $seven[] = strval($d);
            // 当前日期该档案有运势日记
            if ($note_status !== 1 && $d == date('d', $time) && $rid == $selfId['id']) {
                $note_status = 1;
            }
        }

        // 当前日期有运势日记，解锁运势
        if (!$is_unlock && $note_status === 1) {
            $is_unlock = true;
        }

        $unlock_users = getVip($uid);

        if ($unlock_users === 1 || ($unlock_users > 1 && $rid == $selfId['id'])) {
            $is_unlock = true;
            $fortune   = [
                date('d', time()),
                date('d', $times[1]),
                date('d', $times[2]),
                date('d', $times[3]),
                date('d', $times[4]),
                date('d', $times[5]),
                date('d', $times[6]),
                date('d', $times[7]),
            ];
        }

        $data            = [];
        $data['time']    = [];
        $data['fortune'] = [];

        if (date('Y-m', $time) == date('Y-m', time())) {
            $fortune[]       = date('d', time());
            $data['fortune'] = [
                date('d', time()),
            ];
        }

        $arr = [
            'is_unlock'   => $is_unlock,
            'seven'       => $seven,
            'fortune'     => $fortune,
            'selfId'      => $selfId,
            'note_status' => $note_status,
        ];

        return $arr;
    }

}
